package com.qf.fcy.youshare.model;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Alex[FR]
 * Date: 2015/2/7
 * Email: alex_fcy@163.com
 */
public class Category {
    private String id;
    private String name;
    private String url;
    private List<Category> subCategory;

    public Category() {
        subCategory = new LinkedList<Category>();
    }

    public void parseJson(JSONObject json) {
        if (json != null) {
            // TODO 解析
            try {
                id = json.getString("id");
                name = json.getString("name");
                url = json.getString("url");
//                Object obj = json.get("");//然后再判断 obj instance of JSONObject可以么
                // 接口特点，子分类 是数组的都是空的[], 为对象的才有 子分类
                try {
                    // 获取子分类的内容，如果没有子分类了，将会出现一个异常，我们忽略即可
                    JSONObject jsonObject = json.getJSONObject("cat_id");
                    JSONArray names = jsonObject.names();
                    if (names != null) {
                        for (int i = 0; i < names.length(); i++) {
                            String string = names.getString(i);
                            JSONObject object = jsonObject.getJSONObject(string);
                            Category category = new Category();
                            category.parseJson(object);
                            subCategory.add(category);

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<Category> getSubCategory() {
        return subCategory;
    }

    public void setSubCategory(List<Category> subCategory) {
        this.subCategory = subCategory;
    }
}
